<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;


class GoodsCategory extends Model
{

    

    

    // 表名
    protected $name = 'goods_category';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    /**
     * 分类列表
     * @param $data
     * @return array
     */
    public static function getList($data)
    {
        $where = '1=1';
        $order = 'a.weigh desc, a.id desc';
        $page  = isset($data['page']) && !empty($data['page']) ? intval($data['page']) : 1;
        $limit  = isset($data['limit']) && !empty($data['limit']) ? intval($data['limit']) : 10;
        $start = ($page-1)*$limit;
        $field = "a.id, a.name, a.weigh, a.createtime";

        $list = db('goods_category')
            ->alias('a')
            ->where($where)
            ->order($order)
            ->limit($start, $limit)
            ->field($field)
            ->select();
        if(!empty($list))
        {
            foreach ($list as &$v)
            {
                $v['createtimeText'] = isset($v['createtime']) && !empty($v['createtime']) ? date('Y-m-d H:i:s', $v['createtime']) : '';
            }
            unset($v);
        }

        $total = db('goods_category')
            ->alias('a')
            ->where($where)
            ->count();

        return [
            'list' => $list,
            'total' => $total,
        ];

    }

    /**
     * @param $data
     * @return bool
     */
    public static function addRow($data)
    {
        // 添加数据
        $shopData = [
            'name' => $data['name'],
        ];
        if(isset($data['weigh'])) $shopData['weigh'] = $data['weigh'];
        $shopData['createtime'] = time();
        $shopData['updatetime'] = time();

        Db::startTrans();
        try {
            // 店铺添加
            GoodsCategory::insert($shopData, true);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

    /**
     * @param $data
     * @return bool
     */
    public static function updateRow($data)
    {
        // 添加数据
        $shopData = [
            'name' => $data['name'],
        ];
        if(isset($data['weigh'])) $shopData['weigh'] = $data['weigh'];
        $shopData['updatetime'] = time();

        Db::startTrans();
        try {
            // 分类修改
            GoodsCategory::update($shopData, ['id'=>$data['id']]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

    /**
     * @param $data
     * @return bool
     */
    public static function deleteRow($data)
    {
        Db::startTrans();
        try {
            GoodsCategory::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

        return true;
    }

}
